package com.home.wechat_.ext

import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

/**
 *Author: YFS(893145181@qq.com)
 *Time:2021/12/2 10:41
 */

/**
 * 水平方向动画偏移，可用于页面的进场出场动画
 */
fun Modifier.percentOffsetX(percent: Float): Modifier = this.layout { measurable, constraints ->
    val measure = measurable.measure(constraints)
    layout(measure.width, measure.height) {
        val offset = (percent * measure.width).roundToInt()
        measure.placeRelative(offset, 0)
    }
}

/**
 * 扩展Modifier绘制红点角标
 */
fun Modifier.unReadHint(show: Boolean, badgeColor: Color) = this.drawWithContent {
    drawContent()
    if (show) {
        drawIntoCanvas { canvas ->
            val paint = Paint().apply {
                color = badgeColor
            }
            //在右上角(通过偏移量绘制)绘制一个红点
            canvas.drawCircle(
                Offset(size.width - 1.dp.toPx(), 1.dp.toPx()),
                radius = 5.dp.toPx(),
                paint = paint
            )
        }
    }
}